#include "StdAfx.h"
#include "SocketContent.h"
#include "TranMessage.h"

CSocketContent::CSocketContent(void)
{
}


CSocketContent::~CSocketContent(void)
{
}

// SendUserData
bool CSocketContent::SendUserData(const char *  order ,const char *  data ,  char * result,int &ilength)
{
	return SendUserData(m_strServiceIp.c_str(),m_ServicePort,order,data,result,ilength);
}
bool CSocketContent::SendUserData(const char *  ip ,const int port ,const char *  order ,
	const char *  data ,  char * result,int &ilength)
{
	char senddata[1024];
	ZeroMemory(senddata,1024);
	senddata[0]=0x08;
	senddata[1]=0x01;
	senddata[2]=0x02;
	char * pdata = senddata;
	int inum = strlen(data);
	char length[10];
	memset(length,0,sizeof(length));
	sprintf(length,"%d",strlen(data));
	inum = strlen(length);	
	for(int i = 0; i < inum; ++i)
	{
		pdata[11 - inum+i] = length[i];
	}
	pdata+= 11;
	strncpy(pdata,order,4);
	pdata += 4;
	inum = atoi(length);		
	strcpy_s(pdata,inum+1,data);
	pdata += inum;
	pdata[0] = 0x0d ;
	pdata[1] = 0x0a ;
	pdata[2] = 0x00 ;
	CTranMessage m_Message;
	if(!m_Message.SendDataToTcpClient(ip,port,senddata,inum+17,result,inum))
	{
		return false;
	}
	ilength = inum;
	return true;
}


void CSocketContent::AnalysisData(const char * result,int & iresult, int & ierror)
{
	char data[20];
	const char * pdata = result;
	pdata +=11;
	ZeroMemory(data,20);
	strncpy(data,pdata,4);
	iresult = atoi(data);
	pdata +=4;
	ZeroMemory(data,20);
	strncpy(data,pdata,4);
	ierror = atoi(data);
}
